#include <iostream>
#include <thread>

#include "../include/threadsafe_que.h"

threadsafe_que<int> que;

void print(int value)
{
    static std::mutex io_mutex;
    std::lock_guard<std::mutex> lk(io_mutex);
    std::cout << value << std::endl;
}

void push_task()
{
    for (int i = 0; i < 20; i++) {
        que.push(i);
    }
}

void pop_task()
{   
    int value = -1;
    for (int i = 0; i < 10; i++) {
        if (i & 1) {
            que.wait_and_pop(value);
            print(value);
        }
        else {
            auto ptr = que.wait_and_pop();
            print(*ptr);
        }
    }
}

int main(int argc, char* argv[]) 
{
    std::thread t1(push_task);
    std::thread t2(pop_task);
    std::thread t3(pop_task);

    t1.join();
    t2.join();
    t3.join();
    return 0;
}